<?php
/******************************************************
v4 - Management Framework
Copyright (C) 2008  Gary Taylor, gMerc Incorporation
http://www.gmerc.com/v4/

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

gary@gmerc.com
******************************************************/
class index {
  function loaddata() {
    global $masterclass;

    $masterclass->loadjavascript('personnel');

    $masterclass->websiteconfig['title'].=' - Personnel Editor';
  return;
  }

  function display_personnelsearch($containername='',$pg='',$colname='',$searchvalue='') {
    global $masterclass;

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $pg=$_POST['rsargs'][2];
      $colname=$_POST['rsargs'][3];
      $searchvalue=$_POST['rsargs'][4];
    }

    if ($pg<0) { $pg=1; }

/* Create Search Query */
    if ($colname!='' && $searchvalue!='') {
      $arrsearch[]="(`$colname` LIKE '%".$masterclass->allclasses['mysql_functions']->formatmysqlstr($searchvalue)."%')";
    }

    if (!empty($arrsearch)) {
      $strwhere='';
      foreach ($arrsearch as $str) {
        if ($strwhere!='') { $strsearch.=' OR '; }
        $strwhere.=' ('.$str.') ';
      }
    }

    $listcnt=$masterclass->fetch_recordcount('personnel',$strwhere);
    $listpgcount=ceil($listcnt/$masterclass->websiteconfig['entriesperpage']);
    $listarray=$masterclass->fetch_records('personnel',$pg,$strwhere,' `first`,`last`, `updated` DESC ');

    $ret='';

    if (!empty($listarray)) {
      $ret.='<table width="100%" cellpadding="1" cellspacing="0" border="0">';
      $ret.='<tr valign="bottom" class="sitebox_content" style="background-color:#CCCCCC;">';
      if ($colname!='') { $ret.='<td style="border-bottom:1px solid;"><b>'.$colname.'</b></td>'; }
      $ret.='<td style="border-bottom:1px solid;"><b>Name</b></td><td style="border-bottom:1px solid;"><b>Title</b></td><td style="border-bottom:1px solid;"><b>Email</b></td><td style="border-bottom:1px solid;"><b>Phone</b></td><td style="border-bottom:1px solid;"><b>Department</b></td>';
      $ret.='</tr>';
      foreach ($listarray as $result) {
        $name='';$email='';$phone='';

        $name=$result['first'].' '.$result['last'];
        if ($result['emaila']!='') { if ($email!='') { $email.='<br>'; } $email.=$result['emaila']; }
        if ($result['emailb']!='') { if ($email!='') { $email.='<br>'; } $email.=$result['emailb']; }
        if ($result['dayphone']!='') { if ($phone!='') { $phone.='<br>'; } $phone.='day: '.$result['dayphone']; }
        if ($result['nightphone']!='') { if ($phone!='') { $phone.='<br>'; } $phone.='night: '.$result['nightphone']; }
        if ($result['otherphonea']!='') { if ($phone!='') { $phone.='<br>'; } $phone.='1st cell: '.$result['otherphonea']; }
        if ($result['otherphoneb']!='') { if ($phone!='') { $phone.='<br>'; } $phone.='2nd cell: '.$result['otherphoneb']; }

        $ret.='<tr valign="top" OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'personnel/editpersonnel.php?personnelid='.$result['id'].'\',\'personnel'.$result['id'].'\',\'760\',\'480\');" style="cursor:pointer;" class="sitebox_content" OnMouseOver="this.className=\'sitebox_content_mouseover\'" OnMouseOut="this.className=\'sitebox_content\'" >';
        if ($colname!='') { $ret.='<td style="border-bottom:1px solid;">'.$result[$colname].'&nbsp;</td>'; }
        $ret.='<td style="border-bottom:1px solid;">'.$name.'&nbsp;</td>';
        $ret.='<td style="border-bottom:1px solid;">'.$result['title'].'&nbsp;</td>';
        $ret.='<td style="border-bottom:1px solid;">'.$result['companyemail'].'&nbsp;</td>';
        $ret.='<td style="border-bottom:1px solid;">'.$phone.'&nbsp;EXT:'.$result['companyext'].'</td>';
        $ret.='<td style="border-bottom:1px solid;">'.$result['department'].'&nbsp;</td>';
        $ret.='</tr>';
      }
      $ret.='</table>';
    }

    $listdisplay=$ret;

    $masterclass->site['grabdata_vars']['load_list']=$listdisplay;
    $masterclass->site['grabdata_vars']['load_listpage']=$pg;
    $masterclass->site['grabdata_vars']['load_listpagecnt']=$listpgcount;
    $masterclass->site['grabdata_vars']['load_listcnt']=$listcnt;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $ret;
  }

  function display() {
  global $masterclass;

  $searchpersonnel_click="personnel_search('load_personnelsearch',document.getElementById('personnelcol').options[document.getElementById('personnelcol').selectedIndex].value,document.getElementById('personnelval').value);";
  $personnelcolumns=$masterclass->allclasses['mysql_functions']->fetch_tablecolumns('personnel');
  
  $personnelsearch_click='update_personnel_list();';

  $unquieid='personnel_search_container';
  $runcmdpre='';
  $cngpg_cmd="personnel_search('display_personnelsearch','$unquieid',document.getElementById('pagelist$unquieid').selectedIndex+1,document.getElementById('personnelcol').options[document.getElementById('personnelcol').selectedIndex].value,document.getElementById('personnelval').value);";
  $runcmdpost=$cngpg_cmd;
  
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top">
    <td>
<?php
  echo $masterclass->allclasses['websites']->sitebox_pageheader('Search Personnel','');
?>
    </td>
  </tr>
</table>
<?php
$nav='';
$nav.='<div class="sitebox_navigation" style="width:100%;">';
$nav.='<ul id="sitebox_navmenu">';
$nav.='<li OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'personnel/editpersonnel.php?'.$masterclass->trackingvar_query().'\',\'_blank\',\'760\',\'480\');"><a>New Personnel</a></li>';
$nav.='<li OnClick="window.close();"><a>Close</a></li>';
$nav.='</ul>';
$nav.='<br style="clear: left;" />';
$nav.='</div>';
echo $nav;
?>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top">
    <td>
<?php
  $nav='';
  $nav.='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr valign="middle" class="sitebox_navigation">';
  $nav.='<td valign="middle" style="padding-left:2px;text-align:left;"><nobr>Search Personnel By&nbsp;<select name="peronnelcol" id="personnelcol">';
  if (!empty($personnelcolumns)) {
    foreach ($personnelcolumns as $col) { $nav.='<option value="'.$col.'"'; if ($col=='first') { $nav.=' selected '; } $nav.='>'.$col.'</option>'; }
  }
  $nav.='</select>&nbsp;For&nbsp;<input type="text" name="personnelval" id="personnelval" size="15">&nbsp;&nbsp;<input type="button" class="button" OnClick="'.$personnelsearch_click.'" value=" Search "></nobr></td>';
  $nav.='</tr></table>';
  echo $masterclass->allclasses['websites']->sitebox_navigation($nav);
?>
    </td>
  </tr>
</table>
<?php
  echo $masterclass->allclasses['websites']->disp_pages($runcmdpre,$runcmdpost,$unquieid);
?>
<div id="<?php echo $unquieid; ?>"><?php echo $this->display_personnelsearch($unquieid,1); ?></div>
<script language="javascript">
  function update_personnel_list() {
    <?php echo $cngpg_cmd; ?>
  }
</script>
<?php

  return;
  }
}
?>